﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Data;


namespace University.Workshop.DataAccess
{
static public class MedicalSuppliesDataAccess
    {
        static MedicalCenterEntities entities = MedicalCenterEntities.Instance;

        public static bool RegisterNewMedicalSupplies(MedicalSupplies item)
        {
            bool response = false;
            if (item != null)
            {
        //        string query = string.Format("insert into MedicalSupplies values('{0}','{1}','{2}','{3}',{4})", item.MedicalSuppliesId,item.ItemName,item.Lot, item.ExpirationDate.ToString(), item.QuantityAvaliable);
         //       int i = DatabaseManager.Instance.ExecuteNonQuery(query);
          //      if (i > 0)
          //          response = true;
          //      else
          //           response = false;

                entities.MedicalSupplies.Add(item);
               entities.SaveChanges();
               response = true;
            }
            return response;

        }

        public static ICollection<MedicalSupplies> GetMedicalSuppliesForName(string name)
        {
           var medicalSupplies = from m in entities.MedicalSupplies
                                 where m.ItemName.StartsWith(name)
                                 select m;
           return medicalSupplies.ToList();
        }



   //     public static DataTable SearchMeadicalSupplies(string ItemName)
     //   {
     //       DataTable table = new DataTable();
     //       table = DatabaseManager.Instance.ExecuteQuery(string.Format("Select MedicalSuppliesId, ItemName, Lot From Patient Where ItemName = '{0}'", ItemName));
      //      return table;
      //  }

        public static void ModifyQuantity(MedicalSupplies item)
        {
            try
            {
                MedicalSupplies medical = entities.MedicalSupplies.First(m => m.MedicalSuppliesId == item.MedicalSuppliesId);
                medical.QuantityAvailable = item.QuantityAvailable;
                entities.SaveChanges();
            }
            catch(NoNullAllowedException)
            {
            }

        }

        public static ICollection<MedicalSupplies> ObtainAllMedicalSupplies()
        {
            var medicalSupplies = from m in entities.MedicalSupplies
                                  select m;
            return medicalSupplies.ToList();
        }
    }
}